package edu.uoc.tdp.pac3.ui.client;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import edu.uoc.tdp.pac3.client.Client;
import edu.uoc.tdp.pac3.ui.i18n.LanguageUtils;

public class ClientFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JMenu firstMenu;
	private JMenu secondMenu;
	private JMenuBar menuBar;
	
	private Client clientConnection = null;
	
	public ClientFrame(String title, int width, int height) {
		super(title);
		getContentPane().setPreferredSize(new Dimension(width,height));
		//getContentPane().setLayout(new GridLayout(2,2));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//setLocationRelativeTo(null);
		
		initializeMenuBar();
		
		setJMenuBar(menuBar);
	}
	
	private void initializeMenuBar(){
		menuBar = new JMenuBar();
		firstMenu = buildFirstMenu();
		secondMenu = buildSecondMenu();
		secondMenu.setEnabled(false);
		menuBar.add(firstMenu);
		menuBar.add(secondMenu);
	}
	
	private JMenu buildFirstMenu(){
		JMenuItem menuItem;
		JMenu menu = new JMenu(LanguageUtils.getMessage("client.menu.one"));
 
        //a group of JMenuItems
        menuItem = new JMenuItem(LanguageUtils.getMessage("client.menu.one.sub1"));
        menuItem.setActionCommand("connectRMI");
        menuItem.addActionListener(new ClientMenuActionListener(this));
        menu.add(menuItem);

        menuItem = new JMenuItem(LanguageUtils.getMessage("client.menu.one.sub2"));
        menuItem.setActionCommand("exit");
        menuItem.addActionListener(new ClientMenuActionListener(this));
        
        menu.add(menuItem);
        
        return menu;
	}
	
	private JMenu buildSecondMenu(){
		JMenuItem menuItem;
		
		JMenu menu = new JMenu(LanguageUtils.getMessage("client.menu.two"));
        
        //a group of JMenuItems
        menuItem = new JMenuItem(LanguageUtils.getMessage("client.menu.two.sub1"));
        menuItem.setActionCommand("newStock");
        menuItem.addActionListener(new ClientMenuActionListener(this));
        menu.add(menuItem);

        menuItem = new JMenuItem(LanguageUtils.getMessage("client.menu.two.sub2"));
        menuItem.setActionCommand("newRequest");
        menuItem.addActionListener(new ClientMenuActionListener(this));
        menu.add(menuItem);
        
        menuItem = new JMenuItem(LanguageUtils.getMessage("client.menu.two.sub3"));
        menuItem.setActionCommand("resolveRequests");
        menuItem.addActionListener(new ClientMenuActionListener(this));
        menu.add(menuItem);
        
        return menu;
	}
	
	public boolean createClient(){
		boolean creationOk = true;
		try{
			clientConnection = new Client();
		}catch(Exception e){
			creationOk = false;
		}
		return creationOk;
	}
	
	public void setPanel(int code){
		getContentPane().removeAll();
		switch (code) {
		case 1:
			getContentPane().add(new NewStockPanel(clientConnection));
			break;
		case 2:
			getContentPane().add(new ResourceRequestPanel(clientConnection));
			break;
		case 3:
			getContentPane().add(new ResolveRequestPanel(clientConnection));
			break;
		}
		completarFrame();
	}
	
	public Client getClient(){
		return clientConnection;
	}
	
	public void nuevoPanel(JPanel panel) {
		getContentPane().add(panel);
	}
	
	public void completarFrame() {
		pack();	
		setVisible(true);	
		//setResizable(false);
	}	
	
}
